package cn.kgc.easybuy.service.impl;

import cn.kgc.easybuy.pojo.Collect;
import cn.kgc.easybuy.service.CollectService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

/**
 * @program: yingzi-wants-to-sleep
 * @description:
 * @author: 敖成
 * @create: 2025-09-19 20:29
 **/
@Service
public class CollectServiceImpl implements CollectService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<Collect> getCollectList(Integer userId) {
        List<Collect> collectList = new ArrayList<Collect>();
        try {
            BoundListOperations<String, Object> ops = redisTemplate.boundListOps(String.valueOf(userId));
            List<Object> collects = ops.range(0,-1);
            if (collects.isEmpty()) {
                return null;            }
            ObjectMapper mapper = new ObjectMapper();
            for (Object obj : collects) {
                Collect collect = mapper.readValue(obj.toString(),Collect.class);
                collectList.add(collect);
            }
        } catch (JsonMappingException e) {
            throw new RuntimeException(e);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return collectList;
    }

    @Override
    public boolean addCollect(Collect collect) {
        String id = UUID.randomUUID().toString();
        collect.setId(id);
        try {
            String json = new ObjectMapper().writeValueAsString(collect);
            BoundListOperations<String, Object> ops = redisTemplate.boundListOps(String.valueOf(collect.getUserId()));
            Long size = ops.leftPush(json);
            if (size > 6 && size != null ) {
                ops.rightPop();
            }
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }

        return true;
    }

    @Override
    public boolean deleteCollect(String id,Integer userId) {
        List<Object> collectList = new ArrayList<>();
        try {
            BoundListOperations<String, Object> ops = redisTemplate.boundListOps(String.valueOf(userId));
            List<Object> objectList = ops.range(0,-1);
            ObjectMapper mapper = new ObjectMapper();
            for (Object obj : objectList) {
                Collect collect = mapper.readValue(obj.toString(),Collect.class);
                if (!collect.getId().equals(id)) {
                    collectList.add(obj);
                }

            }
            boolean isDel = redisTemplate.delete(String.valueOf(userId));
            if (!collectList.isEmpty()) {
                ops.leftPushAll(collectList.toArray());
            }

        } catch (JsonMappingException e) {
            throw new RuntimeException(e);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }

        return true;
    }

    @Override
    public boolean deleteAll(int userId) {
        Long size = redisTemplate.delete(redisTemplate.keys(String.valueOf(userId)));
        return size > 0 ? true : false;
    }

    @Override
    public boolean checkIsCollect(Integer pid,Integer userId) {
        BoundListOperations<String, Object> ops = redisTemplate.boundListOps(String.valueOf(userId));
        List<Object> objectList = ops.range(0,-1);
        ObjectMapper mapper = new ObjectMapper();
       try {
           if (objectList.isEmpty() || objectList.size() == 0) {
               return true;
           }
           for (Object obj : objectList) {
               Collect collect = mapper.readValue(obj.toString(),Collect.class);
               String id = collect.getProduct().getId();
               if (id.equals(String.valueOf(pid))) {
                   return false;
               }

           }
       }catch (JsonMappingException e) {
           throw new RuntimeException(e);
       } catch (JsonProcessingException e) {
           throw new RuntimeException(e);
       }

        return true;
    }
}
